package com.lianzhihui.romeex.ui.me.dialog;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import static java.lang.String.format;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me.dialog
 * @ClassName: UpDateVersionDialog
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/3 10:40
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/3 10:40
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class UpDateVersionDialog extends BaseDialog {

    UpdateCallback myUpdateCallback;
    @BindView(R.id.tv_version_code)
    TextView tvVersionCode;
    @BindView(R.id.tv_update_context)
    TextView tvUpdateContext;
    @BindView(R.id.tv_update_now)
    TextView tvUpdateNow;
    @BindView(R.id.tv_update_after)
    TextView tvUpdateAfter;
    @BindView(R.id.show_version_info_layout)
    FrameLayout showVersionInfoLayout;
    @BindView(R.id.tv_downloading)
    TextView tvDownloading;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.progressbar_dialog_update)
    ProgressBar progressbarDialogUpdate;
    @BindView(R.id.tv_downloading_txt)
    TextView tvDownloadingTxt;
    @BindView(R.id.tv_download_background)
    TextView tvDownloadBackground;
    @BindView(R.id.download_layout)
    RelativeLayout downloadLayout;

    public UpDateVersionDialog(@NonNull Context context) {
        super(context);
    }

    public UpDateVersionDialog(Context context, int style) {
        super(context, style);
    }

    @Override
    public void setCancelable(boolean flag) {
        super.setCancelable(flag);
    }

    public void setCallback(UpdateCallback myUpdateCallback) {
        this.myUpdateCallback = myUpdateCallback;
    }

    @Override
    public void show() {
        super.show();
        EventBus.getDefault().register(this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_update_version;
    }

    @Override
    protected void initView() {
        progressbarDialogUpdate.setMax(100);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getCode() == AppConstants.Event.UPDATE_VERSION) {
            String value = (String) event.getValue();
            String[] split = value.split(",");
            if (split.length >= 2) {
                String totalBytes = split[0];
                String downloadedBytes = split[1];
                double downloaded = Double.parseDouble(downloadedBytes) / 1048576.00;
                double total = Double.parseDouble(totalBytes) / 1048576.00;
                tvDownloadingTxt.setText(format("%.2fM/%.2fM", downloaded, total));
                int progress = (int) ((Long.parseLong(downloadedBytes) * 100) / Long.parseLong(totalBytes));
                progressbarDialogUpdate.setProgress(progress);
            }
        }
    }


    @OnClick({R.id.tv_update_now, R.id.tv_update_after, R.id.iv_close, R.id.tv_download_background})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.tv_update_now:
                    showVersionInfoLayout.setVisibility(View.GONE);
                    downloadLayout.setVisibility(View.VISIBLE);

                    if (myUpdateCallback != null) {
                        myUpdateCallback.sure();
                    }
                    break;
                case R.id.tv_update_after:
                    if (myUpdateCallback != null) {
                        myUpdateCallback.cancel();
                    }
                    break;
                case R.id.iv_close:
                    dismiss();
                    break;
                case R.id.tv_download_background:
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }


    public interface UpdateCallback {
        void sure();

        void cancel();
    }


}
